//
//  ClassViewController.swift
//  UserApp_iOS
//
//  Created by BestVast on 2021/5/18.
//  Copyright © 2021 linlin. All rights reserved.
//

import UIKit

/// 分类页面
class ClassViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource {
    var tableView: UITableView!
    var letterListView: UITableView!
    var beginTimePicker: PickerDialogView?
    let letters = ["A", "B", "C", "D", "E"]
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.uiConfig()
    }
    
    func uiConfig() {
        self.view.backgroundColor = .white
        self.navigationItem.title = "商家分类"
        
        self.createTableView()
        self.createCollectionView()
        self.createLetterView()
    }
    func createCollectionView() {

        self.view .addSubview(self.collectView)
    }
    func createTableView() {
        tableView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: screenHeight - 64), style: .plain)
        self.view.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView.init()
        tableView.register(ClassTableViewCell.self, forCellReuseIdentifier: "ClassTableViewCell")
        tableView.separatorStyle = .none
    }
    // 创建右侧拼音列表
    func createLetterView() {
        letterListView = UITableView.init(frame: CGRect.init(x: screenWidth - 15, y: 0, width: 12, height: screenHeight - 64), style: .plain)
        self.view.addSubview(letterListView)
        letterListView.delegate = self
        letterListView.dataSource = self
        letterListView.tableFooterView = UIView.init()
        letterListView.register(LetterCell.self, forCellReuseIdentifier: "LetterCell")
        letterListView.separatorStyle = .none
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == letterListView{
            return letters.count
        }
        return 10
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if tableView == letterListView{
            return 15
        }
        return 44
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == letterListView{
            let cell = tableView.dequeueReusableCell(withIdentifier: "LetterCell") as? LetterCell
            if indexPath.row == 2 {
                cell?.selectStatus(text: letters[indexPath.row], select: true)
            } else {
                cell?.selectStatus(text: letters[indexPath.row], select: false)
            }
            return cell!
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClassTableViewCell") as? ClassTableViewCell
        let title = "段志强\(indexPath.row)"
        if indexPath.row == 2 {
            cell?.selectStatus(text: title, select: true)
        } else {
            cell?.selectStatus(text: title, select: false)
        }
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    lazy var collectView:UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize(width: 80 , height: 100)
        layout.minimumLineSpacing=1;
        layout.minimumInteritemSpacing=1
//        layout.footerReferenceSize = CGSize(width: screenWidth - 100, height: 0)
        layout.headerReferenceSize = CGSize(width: screenWidth - 100, height: 26)
        let frame = CGRect.init(x: 110, y: 0, width: screenWidth - 130, height: screenHeight - 64)
        let collectView = UICollectionView.init(frame: frame, collectionViewLayout: layout)
        collectView.backgroundColor = .white
        collectView.delegate = self
        collectView.dataSource = self
        collectView.showsVerticalScrollIndicator = false
        collectView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "SwiftHeaderCollectionReusableView")
        collectView.register(ClassCollectionViewCell.self, forCellWithReuseIdentifier: "ClassCollectionViewCell")
        return collectView
    }()
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 3
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 20
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cellString = "ClassCollectionViewCell"
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellString, for: indexPath) as? ClassCollectionViewCell
        cell?.titleLabel.text = "段志强\(indexPath.row)"

        return cell!
    }
     //设置 区头和区尾
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader  {
            let headerView: UICollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "SwiftHeaderCollectionReusableView", for: indexPath)
            let titleLabel = UILabel.init(frame: CGRect.init(x: 10, y: 0, width: collectionView.frame.size.width - 20, height: 26))
            titleLabel.font = .systemFont(ofSize: 18)
            titleLabel.textColor = .black
            titleLabel.text = "单向建系列"
            headerView.addSubview(titleLabel)
            return headerView
        }
        else
        {
            return UICollectionReusableView.init()
        }
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        if let picker = beginTimePicker{
//            picker.show()
//        } else {
//            beginTimePicker = DatePickerDialogView(confirmHandler: { (date) in
//
//
//            }, cancelHandler: nil)
//            beginTimePicker?.show()
//        }
        beginTimePicker = PickerDialogView.init(confirmHandler: { (year, month, date) in
            
        }, cancelHandler: {
            
        })
        beginTimePicker?.show()
    }
}
